package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubProtocol;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.esign.service.ContBasSnService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.tbill.base.service.TbSndService;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignIntPayKeyword;
import com.winchain.chainbill.util.TranslateUtil;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillIntPayContractSponsorSubcriber extends OnErrorNotImplementedSubscriber<TBillEvent<? extends Object>> {
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private ContService contService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private TbSndService tbSndService;
	@Autowired
	private ContBasSnService contBasSnService;

	public void onApplicationEvent(TBillEvent<?> t) {
		createContForLocal(t);
	}

	private ContBas createContForLocal(TBillEvent<?> t) {
		TbRec tbRec = t.getBill().getRec();
		TbDct tbDct = (TbDct) t.getBill().getSource();

		String bizId = tbDct.getBillId();
		String bizType = "52";
		String protTitle = "����";
		String sponsorId = CommonDataUtil.getCurEntId();
		String acceptorId = CommonDataUtil.getCurEntId();
		String protocolType = "intpaypromise_proto";
		String grpId = tbRec.getGrpId();
		if (tbDct == null) {
			return null;
		}
		PubProtocol protocol = this.pubProtocolFeign.getPubProtocolByTypeAndGrpId(protocolType, grpId, null);
		List<ContTmpl> tmpls = this.contService.getContTmpl(bizType);
		List<ContProt> prots = null;
		CusRec cus = (CusRec) this.cusRecService.selectByKey(sponsorId);
		String contSn = this.contBasSnService.createContSn(cus.getGrpId());
		if ((protocol != null) && (tmpls != null) && (tmpls.size() > 0)) {
			prots = new ArrayList();
			String oriContent = protocol.getContent();
			String content = genContent(oriContent, tbDct, contSn, tbRec);
			ContTmpl tmpl = (ContTmpl) tmpls.get(0);
			ContProt prot = this.contService.genProtWithContent(tmpl.getTmplId(), protTitle, content, true);
			prots.add(prot);
		}
		ContBas cont = new ContBas();
		String contId = SystemUtil.getUUID();
		cont.setContId(contId);
		cont.setContSn(contSn);
		cont.setContTitle(protTitle);
		cont.setBizType(bizType);
		cont.setBizId(bizId);
		cont.setSponsor(sponsorId);
		cont.setAcceptor(acceptorId);
		cont.setContDate(SysDateUtil.getSystemDate());
		cont.setCreateTime(SysDateUtil.getSystemDateTime());
		cont.setContState("1");
		cont.setContProgress("0");
		this.contService.saveCont(cont, prots);
		return cont;
	}

	private String genContent(String content, TbDct source, String contSn, TbRec tbRec) {
		EsignIntPayKeyword keyword = new EsignIntPayKeyword();
		keyword.setIntpayContSn(contSn);
		CusRec seller = (CusRec) this.cusRecService.selectByKey(source.getSellerId());
		keyword.setApplyCompanyName(seller.getCusName());
		CusRec buyer = (CusRec) this.cusRecService.selectByKey(source.getBuyerId());
		keyword.setIntPayCompanyNm(buyer.getCusName());
		keyword.setIntPayLrName(buyer.getLrName());
		TbSnd tbSnd = this.tbSndService.selectByBillId(tbRec.getBillId());

		keyword.setIntPayAcctNm(tbSnd.getConfirmerNm());
		keyword.setIntPayOpenBank(tbSnd.getConfirmerBankNm());
		keyword.setIntPayAcctNo(tbSnd.getConfirmerAcctNo());
		keyword.setTbBillCode(tbRec.getBillCode());
		keyword.setTbBillNum("1");
		try {
			String billType = TranslateUtil.getConstantName("com.winchain.chainbill.constant.TBillConstants", "TbType",
					tbRec.getBillType());
			keyword.setIntPayBillType(billType);
		} catch (Exception e) {
			this.logger.info("����������" + e.getMessage());
		}
		keyword.setTbBillAmt("��" + MoneyUtil.format(tbRec.getBillAmt()));
		keyword.setActualPayAmt("��" + MoneyUtil.format(source.getDctInt()));
		keyword.setIntPayDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		return ContTemplateHelper.replaceContent(content, keyword);
	}
}
